package com.work.mdass.service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.work.mdass.domain.Course;
import com.work.mdass.domain.OrderApplyDetials;
import com.work.mdass.domain.TeachingMaterial;
import com.work.mdass.properties.CourseInterface;
import com.work.mdass.properties.OrderApplyDetialsInterface;
import com.work.mdass.properties.TeachingMaterialInterface;

@Service
public class CourseService {

	@Autowired
	private CourseInterface courseInterface;
	@Autowired
	private TeachingMaterialInterface teachingMaterialInterface;
	@Autowired
	private OrderApplyDetialsInterface orderApplyDetialsInterface;
	
	/*
	 * 添加一门课程
	 */
	public Course addCourse(Course course){
		
		if(!isExit(course.getCourseSerial())){
			return courseInterface.save(course);
		}
		else return null;
			
	}
	
	/*
	 * 通过课程编号删除课程信息
	 */
	
	public String deleteById(Integer id){
		
		courseInterface.delete(id);
		
		return "success";
	}
	/*
	 * 通过id获得课程信息
	 */
	public Course getCourse(Integer id) {
		return courseInterface.findOne(id);
	}
	/*
	 * 通过任课老师查找课程
	 */
	public Map<Object,Object> findByTName(String tName){
		
		List<Course> courses = courseInterface.findByTName(tName);
		
		Map<Object,Object> result = new HashMap<>();
		
		if(courses.size()!=0) {
			result.put("code", 0);
			result.put("msg", "成功");
			result.put("data", courses);
			result.put("count", courses.size());
		}else {
			result.put("code", 1);
			result.put("msg", "没有符合要求的课程");
			result.put("data", null);
			result.put("count",0);
		}
		return result;
	}
	
	/*
	 * 通过课程名称课程
	 */
	
	public Map<Object,Object> findByCourseName(String CourseName){
		
		List<Course> courses = courseInterface.findByCourseName(CourseName);
		
		Map<Object,Object> result = new HashMap<>();
		
		if(courses.size()!=0) {
			result.put("code", 0);
			result.put("msg", "成功");
			result.put("data", courses);
			result.put("count", courses.size());
		}else {
			result.put("code", 1);
			result.put("msg", "没有符合要求的课程");
			result.put("data", null);
			result.put("count",0);
		}
		return result;
	}
	/*
	 * 获得所有课程信息
	 */
	public List<Course> getAllCourse(){
		
		return courseInterface.findAll();
	}
	/*
	 * 修改课程信息
	 */
	public Course updataCourse(Course course) {
		
		return courseInterface.save(course);
	}
	
	/*
	 * 通过关键字进行查找
	 * @return {关键字匹配课程名}
	 */
	public Map<Object,Object> getByKeyWords(String keyWord){
		List<Course> courses = courseInterface.findByCourseNameLike(keyWord);
		
		
		Map<Object,Object> result = new HashMap<>();
		
		if(courses.size()!=0) {
			result.put("code", 0);
			result.put("msg", "成功");
			result.put("data", courses);
			result.put("count", courses.size());
		}else {
			result.put("code", 1);
			result.put("msg", "没有符合要求的课程");
			result.put("data", null);
			result.put("count",0);
		}
		return result;
	}
	/*
	 * 通过两个字段关联查找
	 */
	public Map<Object,Object> forHighcharts(String tName,String classTrem){
		
		System.out.println(tName+".."+classTrem);
		
		List<Course> courses = courseInterface.findByTNameAndClassTremLike(tName, classTrem);
		System.out.println(courses.size());
		
		int materialCountX = 0;
		
		int applyCountX =0;
		
		int materialCountY = 0;
		
		int applyCountY =0;
		
		
		for(int i=0;i<courses.size();i++) {
			
			
			
			List<TeachingMaterial> materialData = teachingMaterialInterface.findByCourseSerial(courses.get(i).getCourseSerial());
			
			
			if(courses.get(i).getClassTrem().substring(11, 15).equals("第一学期")) {
				materialCountX  =materialCountX+ materialData.size();
			}else {
				materialCountY = materialCountY+ materialData.size();
			}
			
			if(orderApplyDetialsInterface.findByCourseName(courses.get(i).getCourseName())!=null) {
				
				if(courses.get(i).getClassTrem().substring(11, 15).equals("第一学期")) {
					applyCountX++;
				}else {
					applyCountY++;
				}
			}
		
		}	
		
		List<Integer> numberX = new ArrayList<>();
		
		numberX.add(materialCountX);
		numberX.add(applyCountX);
		
		List<Integer> numberY = new ArrayList<>();
		
		numberY.add(materialCountY);
		numberX.add(applyCountY);
		
		Map<Object,Object> result = new HashMap<>();
		
		if(courses.size()!=0) {
			result.put("materialCountX", materialCountX);
			result.put("applyCountX",applyCountX);
			result.put("materialCountY", materialCountY);
			result.put("applyCountY",applyCountY);
		}else {
			result.put("materialCountX", materialCountX);
			result.put("applyCountX",applyCountX);
			result.put("materialCountY", materialCountY);
			result.put("applyCountY",applyCountY);
		}
		return result;
	}
	/*
	 * 判断课程编号是否已存在
	 */
	public boolean isExit(String courseSerial){
		
		if(courseInterface.findByCourseSerial(courseSerial)!=null) return true;
		
		else return false;
	}
}
